package org.farng.mp3.object;

/**
 * Title: Description: Copyright:    Copyright (c) 2002 Company:
 *
 * @author Eric Farng
 * @version $Revision: 1.7 $
 */
public class ObjectID3v2LyricLine extends AbstractMP3Object {
    /** DOCUMENT ME! */
    String text = "";

    /** DOCUMENT ME! */
    long timeStamp = 0;

    public ObjectID3v2LyricLine(String identifier) {
        this.identifier = identifier;
    }

    public ObjectID3v2LyricLine(ObjectID3v2LyricLine copyObject) {
        super(copyObject);
        this.text      = new String(copyObject.text);
        this.timeStamp = copyObject.timeStamp;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        return text.length() + 1 + 4;
    }

    public void setText(String text) {
        this.text = text;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getText() {
        return text;
    }

    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public long getTimeStamp() {
        return timeStamp;
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectID3v2LyricLine) == false) {
            return false;
        }

        ObjectID3v2LyricLine object = (ObjectID3v2LyricLine) obj;

        if (this.text.equals(object.text) == false) {
            return false;
        }

        if (this.timeStamp != object.timeStamp) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param arr DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readByteArray(byte[] arr, int offset) {
        if (arr == null) {
            throw new NullPointerException("Byte array is null");
        }

        if ((offset < 0) || (offset >= arr.length)) {
            throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + offset + ", array.length = " + arr.length);
        }

        //offset += ();
        text = new String(arr, offset, arr.length - offset - 4);

        //text = text.substring(0, text.length() - 5);
        timeStamp = 0;

        for (int i = arr.length - 4; i < arr.length; i++) {
            timeStamp <<= 8;
            timeStamp += arr[i];
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        return timeStamp + " " + text;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public byte[] writeByteArray() {
        int    i;
        byte[] arr = new byte[getSize()];

        for (i = 0; i < text.length(); i++) {
            arr[i] = (byte) text.charAt(i);
        }

        arr[i++] = 0;
        arr[i++] = (byte) ((timeStamp & 0xFF000000) >> 24);
        arr[i++] = (byte) ((timeStamp & 0x00FF0000) >> 16);
        arr[i++] = (byte) ((timeStamp & 0x0000FF00) >> 8);
        arr[i++] = (byte) (timeStamp & 0x000000FF);

        return arr;
    }
}